//
//  DnRollMenu.m
//  DnRollTabbarMenu
//
//  Created by Ngoc on 27/11/2011.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "DnRollMenu.h"

@interface DnRollMenu (private) 
-(void) setUp;
-(NSArray*) itemsAtMenuIndex:(NSInteger) menuIndex;
-(NSArray*) itemsOfMenu:(id) sender;
-(void) reloadData;
-(DnRollHorizontalMenuTableCell*) navigationMenu;
-(void) resetCurrentItemState;
@end

@implementation DnRollMenu
@synthesize dataSource =_dataSource;
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUp];
    }
    return self;
}
-(void) setUp
{
    UIImage *backgroundImage = [UIImage imageNamed:kMenuRollBackgroundImageName];
    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, backgroundImage.size.width, backgroundImage.size.height);
    self.backgroundColor = [UIColor colorWithPatternImage:backgroundImage];
}
-(void) layoutSubviews
{
    [self reloadData];
    [self setNeedsDisplay];
}

-(void) changeMenuIndex:(id) sender
{
    
    UIButton *button = (UIButton*) sender;
    NSInteger menuIndex = [_navigationMenuItems indexOfObject:button];
    if(!_currentNavItem)
    {
        // I don't know cause of problem: UIButton can't change state image when assigned selected = YES
        // so I use this trick.
        UIButton *first = (UIButton*) ((DnRollMenuItem *)[_navigationMenuItems objectAtIndex:0]).internalView;
        UIImage *selectedImg = first.currentImage;
        UIImage *normalImg = [first imageForState:UIControlStateSelected];
        [first setImage:normalImg forState:UIControlStateNormal];
        [first setImage:selectedImg forState:UIControlStateSelected];
        first.selected = NO;
        first.highlighted = NO;
    }

    for (int i=0; i< [_navigationMenuItems count]; i++) {
        
        DnRollMenuItem *item = [_navigationMenuItems objectAtIndex:i];
        if(item.internalView == button)
        {
            menuIndex = item.tag;
            if(_currentNavItem)
            {
                if ([_currentNavItem isKindOfClass:[UIButton class]]) {
                    ((UIButton*) _currentNavItem).selected = NO;
                    ((UIButton*) _currentNavItem).highlighted = NO;
                }
            }
            _currentNavItem = item.internalView;
            if(_currentNavItem)
            {
                if ([_currentNavItem isKindOfClass:[UIButton class]]) {
                    ((UIButton*) _currentNavItem).selected = YES;
                    ((UIButton*) _currentNavItem).highlighted = YES;
                }
            }
            break;
        }
        
    }
    if (menuIndex>=0 && menuIndex<[_navigationMenuItems count] ) {
        [_menuTableView goToMenu:menuIndex fromMenu:_currentMenuIndex];
        [self resetCurrentItemState];
        _currentMenuIndex =menuIndex;
        _currentItemIndex =0;

    }
}
-(void) changeButtonBgToSelected:(id) sender
{

    if ([sender isKindOfClass:[UIButton class]]) {
        UIButton *button = (UIButton*) sender;
        [self performSelector:@selector(changeSate:) withObject:button afterDelay:0.0f];
        
    }
}
-(void) resetCurrentItemState
{
    UIView *currentView = [[self itemsAtMenuIndex:_currentMenuIndex] objectAtIndex:_currentItemIndex];
    if ([currentView isKindOfClass:[DnRollMenuItem class]]) {
        DnRollMenuItem *item = (DnRollMenuItem*)currentView;
        if ([item.internalView isKindOfClass:[UIButton class]]) {
            UIButton *tmpBtn = (UIButton*) item.internalView;
            tmpBtn.selected = NO;
            tmpBtn.highlighted = NO;
        }
    }
}
-(void) changeSate:(UIButton*) button
{   
    [self resetCurrentItemState];
    if (button.selected) {
        button.selected = NO;
        button.highlighted= NO;
    }
    else
    {
        button.selected = YES;
        button.highlighted = YES;
    }
    _currentItemIndex = button.tag;
}

-(NSArray*) itemsAtMenuIndex:(NSInteger) menuIndex
{
    return [_allMenuItems objectAtIndex:menuIndex];
    
}
-(NSArray*) itemsOfMenu:(id) sender
{
    NSArray *items;
    if (sender == [self navigationMenu]) {
        items = _navigationMenuItems;
    }
    else
    {
    NSInteger menuIndex = [_menus indexOfObject:sender];
    items= [self itemsAtMenuIndex:menuIndex];
    }
    return items;
}
-(DnRollHorizontalMenuTableCell*) navigationMenu
{
    return [_navigationMenus objectAtIndex:0];
}
//==========================================================================================
// Menus
-(NSInteger) numberMenuOfTable:(id) sender
{
    NSInteger count;
    if (sender == _navigationView) {
        count =0;
    }
    else count = [_menus count];
    return count;
}
-(UITableViewCell*)menuSection:(id)sender atIndex:(NSInteger)menuIndex
{
    DnRollHorizontalMenuTableCell* menu;
    if (sender == _navigationView) {
        menu = [self navigationMenu];
    }
    else menu = [_menus objectAtIndex:menuIndex];
    return menu;
}
-(NSInteger) indexOfMenu:(id) sender
{
    NSInteger menuIndex;
    if (sender == [self navigationMenu]) {
        menuIndex =0;
    }
    else menuIndex = [_menus indexOfObject:sender];
    return menuIndex;
}
//==========================================================================================
// Horizontal
-(NSInteger) numberItemsOfHorizontalMenu:(id) sender
{
    NSInteger count =0;
    if (sender == [self navigationMenu] ) {
       count = [_navigationMenuItems count]; 
        //NSLog(@"Nav: %d", count);
    }
    else
    {
        count = [[self itemsOfMenu:sender]count];
    }
    return count;
}
-(UITableViewCell*) horizontalMenu:(id) sender itemAtIndex:(NSInteger) itemIndex
{
    NSArray *items = [self itemsOfMenu:sender];
    DnRollMenuItem* item = [items objectAtIndex:itemIndex];
    //NSLog(@"Item: %d",itemIndex);
    return item;
}
//==========================================================================================

-(void) reloadData
{
    NSInteger numberMenu, numberItems;
    NSMutableArray *menuItems;
    if (self.dataSource) {
        _allMenuItems = [[NSMutableArray alloc]init];
        _menus = [[NSMutableArray alloc]init];
        numberMenu = [self.dataSource numberOfMenus];
        for (int menuIndex =0 ; menuIndex<numberMenu; menuIndex++) 
        {
            numberItems = [self.dataSource numberItemsOfMenu:menuIndex];
            menuItems = [NSMutableArray array];
            for (int itemIndex=0; itemIndex<numberItems; itemIndex++) 
            {
                UIView *customView = [self.dataSource menu:menuIndex itemAtIndex:itemIndex];
                customView.tag = itemIndex;
                if ([customView isKindOfClass:[UIButton class]]) {
                    [(UIButton*)customView addTarget:self action:@selector(changeButtonBgToSelected:) forControlEvents:UIControlEventTouchUpInside];
                }
                
                DnRollMenuItem *item = [[DnRollMenuItem alloc] initWithFrame:CGRectMake(0, 0, kMenuItemWidth, kMenuItemHeight) customView:customView];
                item.tag =itemIndex;
                [menuItems addObject:item];
            }
            [_allMenuItems addObject:menuItems];    
            DnRollHorizontalMenuTableCell *hMenu = 
            hMenu = [[DnRollHorizontalMenuTableCell alloc]initWithFrame:CGRectMake(0, 0, 320, 416)]; 
            hMenu.distance =kMenuDistanceBetweenItem;
            hMenu.dataSource = self;
            [_menus addObject:hMenu];
        }
        
        _menuTableView = [[[DnRollMenuTableView alloc]initWithFrame:CGRectMake(8, 9, self.frame.size.width-16, 44) distanceBetweenItems:kMenuDistanceBetweenItem]autorelease];
        _menuTableView.dataSource = self;
        [self addSubview:_menuTableView];
        
        
        // Build Nagivation
        _navigationMenuItems = [[NSMutableArray alloc]init];
        _navigationMenus = [[NSMutableArray alloc]init ];
        _navigationView = [[DnRollMenuTableView alloc]initWithFrame:CGRectMake(4, _menuTableView.frame.origin.y + _menuTableView.frame.size.height+7, self.frame.size.width-8, kMenuNavigationHeight)distanceBetweenItems:0];
        _navigationView.dataSource = self;
       [self addSubview:_navigationView];

        UIView *navItemView;
        for (int itemIndex=0; itemIndex< numberMenu; itemIndex++) {
            navItemView = [self.dataSource navigationMenuItemAtIndex:itemIndex];
            if (navItemView) {
                if ([navItemView isKindOfClass:[UIButton class]]) {
                    [(UIButton*)navItemView addTarget:self action:@selector(changeMenuIndex:) forControlEvents:UIControlEventTouchUpInside];
                }
                
                navItemView.tag = itemIndex;
            DnRollMenuItem *item = [[DnRollMenuItem alloc] initWithFrame:CGRectMake(0, 0, kMenuItemWidth, kMenuItemHeight) customView:navItemView];
                item.tag = itemIndex;
            [_navigationMenuItems insertObject:item atIndex:itemIndex];
            }
        }
        DnRollHorizontalMenuTableCell *navMenu;
        navMenu = [[DnRollHorizontalMenuTableCell alloc]initWithFrame:CGRectMake(0, 0, 320, 416)]; 
        navMenu.distance =0;
        navMenu.dataSource = self;
        [_navigationMenus addObject:navMenu];
    
    }
    

}
//==========================================================================================
-(void) changeToFirstMenu
{
    //_currentMenuIndex =-1;
//    _currentNavItem = ((DnRollMenuItem *)[_navigationMenuItems objectAtIndex:0]).internalView;
//    [self changeMenuIndex:_currentNavItem];
    UIButton *firstBtn = (UIButton*)((DnRollMenuItem*)[_navigationMenuItems objectAtIndex:0]).internalView;
    [firstBtn sendActionsForControlEvents:UIControlEventTouchUpInside];   
}

- (void)dealloc
{
    [super dealloc];
}

@end
